package com.xry.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xry.common.bean.User;
import com.xry.common.service.UserService;
import com.xry.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @GetMapping("{id}")
    public Result getUserById(@PathVariable("id") Integer id) {
        try{
            Object obj=userService.getUserById(id);
            return Result.success("查询成功", obj);
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }

    @GetMapping("/username/{username}")
    public Result getUserByUserName(@PathVariable("username") String username) {
        User user = userService.getUserByUserName(username);
        try {
            if(user==null){
                return Result.fail(500,"用户不存在");
            }
            return Result.success("查询成功", user);
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }
    @PostMapping("/addUser")
    public Result addUser(@RequestBody User user) {
        try{
            userService.addUser(user);
            user = userService.getUserById(user.getId());
            return Result.success("新增成功", user);
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }
    @PutMapping("/updateUserById")
    public Result updateUserById(@RequestBody User user) {
        try{
            boolean flag=userService.updateUserById(user);
            if(flag){
                return Result.success( user);
            }else{
                return Result.fail(500,"修改失败");
            }
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }
    @DeleteMapping("{id}")
    public Result deleteUserById(@PathVariable("id") Integer id) {
        try{
            boolean flag=userService.deleteUserById(id);
            if(flag){
                User user = userService.getUserById(id);
                return Result.success("删除成功", user);
            }else{
                return Result.fail(500,"删除失败");
            }
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }
    @GetMapping("/getAll")
    public Result getPage() {
        try{
            Object obj=userService.getAll();
            return Result.success("查询成功", obj);
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }
    @GetMapping("/getPage")
    public Result getPage(@RequestParam(defaultValue = "1") Long current,
                          @RequestParam(defaultValue = "10") Long size,
                          User user) {
        try{
            // 创建分页对象
            IPage<User> page = new Page<>(current, size);
            Object obj = userService.getPage(user, page);
            return Result.success("查询成功", obj);
        }catch (Exception e){
            return Result.fail(500,e.getMessage());
        }
    }


}
